const {
  $Toast
} = require('../../utils/iview/base/index');
const Tools = require('../../utils/modules/Tools');
import NetWork from '../../utils/modules/NetWork';

const app = getApp();


Page({
  data: {
    auth: true,
    isloade: {
      hidden: false
    },
    _baseUrl: app.globalData._baseUrl,
    _imgPath: app.globalData._imgPath,
    _imgPaths: app.globalData._imgPaths,
    _backImg: app.globalData._backImg,
    img: [],
    user_type: 1,
    designer_id: '',
    openid: app.globalData.openid,
    shopName: '',
    shopId: '',
    userInfo: '',
    user: {},
    // phone:'',
    // website:' '
  },
  onLoad(e) {

    var _that = this
    _that.setTitle('我的');
    
  },
  



  users(){
    var _that = this;
    var token = wx.getStorageSync('token');
    if (!token){
      return;
    }
    var net = new NetWork(_that.data._baseUrl);
    net._request({
      url:'api/v1/member/getMember',
      data: { token: token },
      method:'POST',
    },'',res=>{
      if (res.data.error_code == 0){
        _that.setData({
          user: res.data.data
        })
        // 这里设置用户缓存是为了给下单页面调取用户名称
          wx.setStorageSync("user", _that.data.user)
      }
    },res=>{
      if (res.data.error_code == 400 && res.data.msg == 'token无效或已过期') {
        //token过期 重新执行该函数
        app.refreshtoken(token);
        setTimeout(()=>{
          _that.users();
        },1500)
      }
    })
  },

  contact() {
    var _that = this;
    
    var net = new NetWork(_that.data._baseUrl);
    net._request({
      url: 'api/v1/company-contact',
      data: { 
        
        },
      method: 'GET',
    }, '', res => {
      if (res.data.error_code == 0) {
        _that.setData({
          cont: res.data.data
        })
        console.log(res.data.data,"cotn")
 
      }
    }, res => {
      if (res.data.error_code == 400 && res.data.msg == 'token无效或已过期') {
        //token过期 重新执行该函数
        app.refreshtoken(token);
        setTimeout(() => {
          _that.contact();
        }, 1500)
      }
    })
  },






  scan(){
    wx.scanCode({
      success(res) {
        console.log(res)
        // 这里对扫码的数据进行裁剪，扫码功能能在那里，裁剪数据就在那里
        var path = decodeURIComponent(res.path)
        path = path.split('=')[2];
        console.log(path, "数据1")
        //扫码成功之后跳去那个下单的页面和把id也传过去，然后下单的那个页面有两个id
        // （一个是其他设备扫，一个是这个页面扫现在path传过去的id就是这个页面扫）要区分一下
        wx.navigateTo({
          url: '../purchase/purchase?id=' + path ,
        })
      }
    })
  },





// 清除用户缓存，以防串用户
  clearh(){
    wx.removeStorageSync('token');
    wx.removeStorageSync('user');
    this.setData({
      user:""
    })

    $Toast({
      content: '清除成功',
      type: 'success'
    })
  },

// 如果还没登录授权点击意见反馈让它先登录
  goproposal() {
    if (this.data.user.username){
      wx.navigateTo({
        url: './proposal/proposal',
      })
    }
    else {
      $Toast({
        content: '请先登录授权',
        type: 'warning'
      })
    }
    
  },

  gowebsite(){
    wx.navigateTo({
      url: '../website/website',
      // url: '../website/website?wan=' + this.data.cont.company_website,
    })
  },


  savingsRecord(){
    wx.navigateTo({
      url: './savingsRecord/savingsRecord',
    })
  },
  recharge(){
    wx.navigateTo({
      url: './recharge/recharge',
    })
  },
  orderList(){
    wx.navigateTo({
      url: './orderList/orderList',
    })
  },
  onShow: function() {
    var _that = this;
    _that.users();
    _that.contact();
    // _that.isUserInfo();

  },
  modifyUser(){
    wx.navigateTo({
      url: './myInfo/myInfo?user=' + JSON.stringify(this.data.user)
    })
  },
  scanCode: function () {
    var that = this;
    wx.scanCode({ //扫描API
      success(res) { //扫描成功
        console.log(res) //输出回调信息
        that.setData({
          scanCodeMsg: res.result
        });
        wx.showToast({
          title: '成功',
          duration: 1000
        })
      }
    })
  },




  /* 拨打电话 */
  telPhone() {
  	var that = this;
  	wx.makePhoneCall({
      phoneNumber: that.data.cont.customer_service_phone
  	}, res => {

  	});
  },
  /* 修改头部标题 */
  setTitle(str) {
    wx.setNavigationBarTitle({
      title: str,
      success: function() {

      }
    })
  },
  setType(e) {
    var index = e.target.dataset.index;
    this.setData({
      roomType: index
    })
  },
  onShareAppMessage(str) {
  	return {
  		title: str,
  		path: this.route
  	}
  },
  isUserInfo() {
    var _that = this;
    _that.setData({
      auth: false
    })
    return;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          _that.setData({
            auth: false
          })
        }else{
          _that.setData({
            auth: true
          })
        }
      }
    })
  },

  userInfo(e) {
    let _userInfo = e.detail.userInfo;
    // console.log(_userInfo,'e');
    let _that = this;
    let net = new NetWork(_that.data._baseUrl);
    if (_userInfo) {
      this.setData({
        auth: true
      })
    }
    var { signature, rawData, iv, encryptedData } = e.detail;
    wx.login({
      success(res) {
        // console.log('小程序登录信息', res)
        if (res.code) {
          net._request({
            url: 'api/v1/member/wxlogin',
            method: 'POST',
            data: {
              code: res.code,
              signature: signature,
              rawData: rawData,
              iv: iv,
              encryptedData: encryptedData
            },
          }, false, sres => {
            if (sres.data.error_code == 0) {

              var _data = sres.data.data;
              wx.setStorageSync("token", _data.access_token)
              _that.setData({
                token: _data.access_token
              })
              
              _that.users();
            }
          });
        }
      }
    })
  },





 
})